<?php
class Model
{
    protected $table  = '';
    protected $pk     = '';
    protected $fields = [];
    protected $db     = null;

    protected $data;
    protected $column = '*';


    public $succ = ['errorCode'=>0,'errorMessage'=>''];

    public function __construct()
    {
        $this->db = new DB();
        $this->parseTable();
    }

    public function __set($k, $v)
    {
        $this->data[$k] = $v;
    }

    public function __get($k)
    {
        return $this->data[$k];
    }

    public static function table($ta)
    {
        return $ta;
    }
    //分析表名
    public function parseTable()
    {
        $this->table = substr(static::class, 0, -5);
        $sql         = 'desc ' . 'oD_' . $this->table;

        $struct = $this->db->getAll($sql);

        foreach ($struct as $row) {
            $this->fields[] = $row['Field'];

            if ($row['Key'] === 'PRI') {
                $this->pk = $row['Field'];
            }

        }
    }

    public function fields($cols = '*')
    {
        $this->column = $cols;
        return $this;
    }

    public function select()
    {
        return $this->data = $this->db->getAll('select ' . $this->column . ' from oD_' . $this->table);
    }

    public function find($id)
    {
        return $this->data = $this->db->getRow('select ' . $this->column . ' from oD_' . $this->table . ' where ' . $this->pk . '=?', [$id]);
    }

    public function add()
    {
        $sql = 'insert into oD_' . $this->table . ' (' . implode(',', array_keys($this->data)) . ') values (' . str_repeat('?,', count($this->data));
        $sql = substr($sql, 0, -1) . ')';
        // echo $sql;exit;

        return $this->db->insert($sql, array_values($this->data));
    }

    public function remove($t,$id,$name){
        $sql = 'delete from oD_' . $t . " where $name=$id";
        return $this->db->exec($sql);
    }

    /**
     * 获得文件扩展名
     * @param string $filename 上传文件名
     * @return string 返回扩展名
     */
    function getExt($filename) {
        $arr = explode('.',basename($filename));

        return end($arr);
    }

    /**
     * 获得文件唯一扩展名
     * @return string 经过MD5后生成32位唯一的上传文件名
     */
    function getUniName() {
        return md5(uniqid(microtime(true),true));
    }

    function uploadFile($file,$path='./uploads',$max_size,$allowExt) {
        $filename = $file['name'];
        $type = $file['type'];
        $temp_name = $file['tmp_name'];
        $error = $file['error'];
        $size = $file['size'];
        $res = [];

        if($error == UPLOAD_ERR_OK) {
            if ($size>$max_size) {
                $res['mes'] = $filename.'文件超过规定上传大小';
            }
        
            $ext = $this->getExt($filename);
            if(!in_array($ext,$allowExt)) {
                $res['mes'] = $filename.'文件名不合规范';
            }
            if(!is_uploaded_file($temp_name)) {
                $res['mes'] = $filename.'文件不是通过HTTP POST 方法上传过来的';
            }

            if($res) {
                return $res;
            }

            if(!file_exists($path)) {
                mkdir($path,0777,true);
                chmod($path,0777);
            }
            $fname = $this->getUniName();

            $destination = $path.'/'.$fname.'.'.$ext;
            if(move_uploaded_file($temp_name,$destination)) {
                $res['mes'] = $filename.'上传成功';
                $res['dest'] = $destination;
            }else{
                $res['mes'] = $filename.'文件上传失败';
            }
        }else{
            switch ($error) {
            case '1':
            $res['mes']="超过了配置文件上传文件的大小";
            break;
            case '2':
            $res['mes']="超过表单设置上传文件文件的大小";
            break;
            case '3':
            $res['mes']="文件部分被上传";
            break;
            case '4':
            $res['mes']="没有文件被上传";

            break;
            case '6':
            $res['mes']="没有找到临时目录";
            break;
            case '7':
            $res['mes']="文件不可写";

            break;
            default:
            $res['mes']="上传文件失败";
            break;
            }
        }

        return $res;
    }
}
